
/*
 * Postit.java
 */
package organiser;

import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Arrays;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

/**
 * @author
 */
public class Postit extends JPanel implements ActionListener, MouseMotionListener, MouseListener {
    private Home home;
    private Keyboard keyboard;
    private JPanel cards;
    private CardLayout cl;
    private JPanel BackgroundPanel = new JPanel();
    private ArrayList<JTextArea> postIt = new ArrayList<JTextArea>();
    private JButton addPostBtn = new JButton("+");
    private ArrayList<JButton> removePost = new ArrayList<JButton>();
    private ImageIcon background = new ImageIcon("C:/post.jpg");
    private int startX, startY;
    private MysqlDB sql = new MysqlDB();

    public Postit(Home home, Keyboard keyboard) {
        this.home = home;
        this.keyboard = keyboard;
        BackgroundPanel.setSize(800, 480);
        addPostBtn.setBackground(new Color(248, 255, 102));
        addPostBtn.setBounds(760, 190, 40, 60);
        addPostBtn.setVerticalTextPosition(SwingConstants.CENTER);
        addPostBtn.setMargin(new Insets(0, 0, 0, 0));
        addPostBtn.setFont(new Font("Sans-serif", Font.PLAIN, 26));
        addPostBtn.setBorder(BorderFactory.createLineBorder(Color.GRAY, 2));
        addPostBtn.setFocusable(false);
        addPostBtn.addMouseListener(this);

        BackgroundPanel.setLayout(null);
        BackgroundPanel.setBackground(Color.WHITE);

        addPost();
        BackgroundPanel.add(addPostBtn);

    }

    public void addPost() {     
        postIt.add(new JTextArea(""));
        removePost.add(new JButton("X"));
        int i = postIt.size() - 1;
        removePost.get(i).setFont(new Font("Sans-Serif", Font.PLAIN, 18));
        removePost.get(i).setBounds(160, 0, 30, 30);
        removePost.get(i).setBorderPainted(false);
        removePost.get(i).setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
        removePost.get(i).setContentAreaFilled(false);
        removePost.get(i).setFocusable(false);
        removePost.get(i).addMouseListener(this);
        postIt.get(i).setBounds(20, 20, 190, 200);
        postIt.get(i).setSize(190, 200);
        postIt.get(i).setEditable(false);
        postIt.get(i).setPreferredSize(new Dimension(190, 200));
        postIt.get(i).setMargin(new Insets(30, 5, 5, 5));
        postIt.get(i).addMouseMotionListener(this);
        postIt.get(i).addMouseListener(this);
        postIt.get(i).setBackground(new Color(248, 255, 102));
        postIt.get(i).setFont(new Font("Serif", Font.PLAIN, 14));
        postIt.get(i).add(removePost.get(i));
        Border empty = BorderFactory.createEmptyBorder(25, 5, 5, 5);
        Border bevel = BorderFactory.createBevelBorder(BevelBorder.RAISED);
        Border compound = BorderFactory.createCompoundBorder(bevel, empty);
        postIt.get(i).setBorder(compound);
        postIt.get(i).setWrapStyleWord(true);
        postIt.get(i).setLineWrap(true);
        BackgroundPanel.add(postIt.get(i));
        System.out.println(i+": "+postIt.get(i).getComponentZOrder(postIt.get(i)));
        BackgroundPanel.updateUI();
    }

    public JPanel getJPanel(){
        return BackgroundPanel;
    }
    
    public String getText(JTextArea postIt){
        return postIt.getText();
    }



//    public void setOnTop(JTextArea inPostIt){
//        JTextArea temp = inPostIt;
//        System.out.println("max: "+max);
//        int toPos = inPostIt.getComponentZOrder(inPostIt);
//        for(int i = postIt.size(); i>=toPos; i--){
//
//
//            postIt.get(i).setComponentZOrder(temp, i);
//
//        }
//
//
//    }

//    public static void main(String[] args) {
//        Postit p = new Postit();
//    }

    public void actionPerformed(ActionEvent e) {
    }

    public void mouseDragged(MouseEvent e) {
        JTextArea postIt = (JTextArea) e.getSource();
        int x = postIt.getLocation().x + (e.getX() - startX);
        int y = postIt.getLocation().y + (e.getY() - startY);
        postIt.setLocation(x, y);
    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
        cards = home.getCards();
        cl = (CardLayout)(cards.getLayout());
        // FÖR POSTIT
        for (int i = 0; i < postIt.size(); i++) {
            if (e.getSource() == postIt.get(i)) {
                Object[] values = {new Float(0.5), new Float(0.8), Color.LIGHT_GRAY, Color.BLACK, Color.WHITE};
                UIManager.put("Button.gradient", Arrays.asList(values));
                
                System.out.println(postIt.get(i).getText());
                keyboard.setText(postIt.get(i).getText());
                cl.show(cards, "Keyboard");
                //EDIT BUTTON
            }
            if (e.getSource() == removePost.get(i)) {
                BackgroundPanel.remove(postIt.get(i));
                postIt.remove(i);
                removePost.remove(i);
                BackgroundPanel.updateUI();
                //REMOVE BUTTON
            }
        }
        // FÖR ÖVRIGA
        if (e.getSource() == addPostBtn) {
            addPost();
        }
    }

    public void mousePressed(MouseEvent e) {
        startX = e.getX();
        startY = e.getY();
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }
}
